package com.dayouzi.crawler_data.config;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class MysqlSession {

    public SqlSession sqlSession;

    public MysqlSession(DataSourceEnvironment environment) {
        synchronized (this) {
            if (null == sqlSession) {
                SqlSessionFactory factory = DataSourceSqlSessionFactory.getSqlSessionFactory(environment);
                sqlSession = factory.openSession();
            }
        }
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                closeSession();
            }
        });
    }

    /**
     * 释放sqlSession
     */
    public void closeSession() {
        sqlSession.close();
    }

}
